#include "recent_files_manager.h"
#include <QCoreApplication>
#include <QDebug>
#include <QFileInfo>
#include <QSettings>
#include <QApplication>
#include "confighandler.h"
#include "enum_file.h"

RecentFilesManager::RecentFilesManager(QObject *parent)
    : QObject(parent)
{
    initRecentFiles();
}

RecentFilesManager::~RecentFilesManager()
{

}

const QVector<RecentFile *> &RecentFilesManager::recentFiles() const
{
    return m_recentFiles;
}

void RecentFilesManager::initRecentFiles()
{
    QString recentFiles = GlobalConfig::GetInstance()->readConfig(CommonEnum::ConfigParam::RECENTOPENFILE_TEXT).toString();
    if( recentFiles == "" ){
        return;
    }

    QStringList recentFileList = recentFiles.split(";");
    for(int i = 0; i < recentFileList.size(); i++){
        QString recentItem = recentFileList.at(i);
        QStringList recentFileName_FileList = recentItem.split(",");
        pushRecentFile(recentFileName_FileList.at(1), recentFileName_FileList.at(0));
    }
}

void RecentFilesManager::recordRecentFiles()
{

    if(m_recentFiles.size() == 0){
        return;
    }

    QString recentFiles;
    for(int i = m_recentFiles.size() - 1; i >= 0 ; i--){
        recentFiles.append(m_recentFiles.at(i)->modelName());
        recentFiles.append(",");
        recentFiles.append(m_recentFiles.at(i)->modelPath());
        if(i != 0) { recentFiles.append(";"); }
    }

    GlobalConfig::GetInstance()->writeConfig(CommonEnum::ConfigParam::RECENTOPENFILE_TEXT, recentFiles);
}

void RecentFilesManager::setRecentFiles(const QVector<RecentFile *> &newRecentFiles)
{
    if (m_recentFiles == newRecentFiles)
        return;
    m_recentFiles = newRecentFiles;
    emit recentFilesChanged();
}

bool RecentFilesManager::isFileExist(QString fullFileName)
{
    QFileInfo fileInfo(fullFileName);
    if(fileInfo.isFile()) {
        return true;
    }
    return false;
}

int RecentFilesManager::getRecentFilesSize()
{
    int recentFileCount = m_recentFiles.size();
    for( int i = recentFileCount - 1; i >= 0; i-- ){
        QString fileDir = m_recentFiles[i]->modelPath();
        if( !isFileExist(fileDir) ){
            m_recentFiles.removeAt(i);
        }
    }
    return m_recentFiles.size();
}

void RecentFilesManager::pushRecentFile( QString modelPath, QString modelName )
{
    for(int i = 0; i < m_recentFiles.length(); i++){
        if(m_recentFiles[i]->modelPath() == modelPath){
            RecentFile* tmp = m_recentFiles[i];
            for(int j = i; j > 0; j--){
                m_recentFiles[j] = m_recentFiles[j - 1];
            }
            m_recentFiles[0] = tmp;
            recordRecentFiles();
            return;
        }
    }

    QFileInfo fileInfo(modelPath);
    modelName = modelName == "0" ? fileInfo.fileName() : modelName;
    RecentFile* rf = new RecentFile(this);
    rf->setModelName(modelName);
    rf->setModelPath(modelPath);
    if(m_recentFiles.length() == RF_MAX_SIZE){
        m_recentFiles.pop_back();
    }
    m_recentFiles.push_front(rf);

    recordRecentFiles();
}


